﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration;
using System.Data.Entity.ModelConfiguration.Configuration;
using System.Data.Objects.DataClasses;
using System.Linq.Expressions;
using System.Reflection;
using Grundy.Common;
using Grundy.CryptographicServices;
using Grundy.Entity;
using Grundy.Message;

namespace Grundy.Administration
{
    public class EntitlementsDatabase : DbContext
    {
        public EntitlementsDatabase(string connectionStringName) : base(connectionStringName)
        {
            this.Database.ExecuteSqlCommand("SELECT NEWID()");
            this.Configuration.LazyLoadingEnabled = false;
            this.Configuration.ProxyCreationEnabled = false;
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            // Node
            modelBuilder.Entity<Node>().Property(i => i.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            // License
            modelBuilder.Entity<License>().Property(i => i.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            
            // FloatingLicense
            modelBuilder.Entity<FloatingLicense>().Ignore(i => i.Client);

            // CountedFloatingLicense
            modelBuilder.Entity<CountedFloatingLicense>().Ignore(i => i.LimitType);

            // Customer
            modelBuilder.Entity<Customer>().Property(i => i.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            // CustomerEntitlement
            modelBuilder.Entity<CustomerEntitlement>().Property(i => i.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            // LicenseEntitlement
            modelBuilder.Entity<LicenseEntitlement>().Property(i => i.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            
            // Feature
            modelBuilder.Entity<Feature>().Property(i => i.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
            
            // Product
            modelBuilder.Entity<Product>().Property(i => i.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

            // FeatureIdentificationKey
            modelBuilder.ComplexType<FeatureIdentificationKey>();
        }

        public virtual DbSet<Product> Products { get; set; }
        public virtual DbSet<Feature> Features { get; set; }
        public virtual DbSet<Customer> Customers { get; set; }
        public virtual DbSet<License> Licenses { get; set; }
        public virtual DbSet<LicenseEntitlement> LicenseEntitlements { get; set; }
        public virtual DbSet<CustomerEntitlement> CustomerEntitlements { get; set; }
        public virtual DbSet<Node> Nodes { get; set; }
        public virtual DbSet<Identifier> Identifiers { get; set; }
           
    }
}